<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Plate Creation Test (Auto-Filled)</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f5f5f5;
        }
        textarea {
            height: 100px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
        #status {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
        .auto-filled {
            color: #666;
            font-style: italic;
        }
    </style>
</head>
<body>
<h1>Create New Plate (Auto-Filled Test)</h1>
<div class="auto-filled">所有字段已自动填充，直接点击提交即可</div>

<form id="plateForm">
    <div class="form-group">
        <label for="plateName">Plate Name:</label>
        <input type="text" id="plateName" name="plateName" readonly>
    </div>

    <div class="form-group">
        <label for="plateDescription">Plate Description:</label>
        <textarea id="plateDescription" name="plateDescription" readonly></textarea>
    </div>

    <div class="form-group">
        <label for="moderatorId">Moderator ID:</label>
        <input type="text" id="moderatorId" name="moderatorId" readonly>
    </div>

    <div class="form-group">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" readonly>
    </div>

    <div class="form-group">
        <label for="files">Upload Files (可选):</label>
        <input type="file" id="files" name="files" multiple>
    </div>

    <button type="button" id="submitBtn">提交测试请求</button>
</form>

<div id="status"></div>

<script>
    $(document).ready(function() {
        // 硬编码自动填充数据
        const testData = {
            plateName: "测试板块_" + Math.floor(Math.random() * 1000),
            plateDescription: "这是自动生成的测试板块描述，创建时间: " + new Date().toLocaleString(),
            moderatorId: 1001,
            username: "auto_test_user"
        };

        // 填充表单字段
        $('#plateName').val(testData.plateName);
        $('#plateDescription').val(testData.plateDescription);
        $('#moderatorId').val(testData.moderatorId);
        $('#username').val(testData.username);

        // 提交按钮点击事件
        $('#submitBtn').click(function() {
            // 创建FormData对象
            var formData = new FormData();

            // 添加plate数据
            var plate = {
                plateName: $('#plateName').val(),
                plateDescription: $('#plateDescription').val(),
                moderatorId: parseInt($('#moderatorId').val()),
            };
            const plateBlob = new Blob([JSON.stringify(plate)], { type: 'application/json' });
            formData.append("plate", plateBlob);

            // 添加username
            formData.append("username", $('#username').val());

            // 添加文件（如果有）
            var files = $('#files')[0].files;
            for (var i = 0; i < files.length; i++) {
                formData.append("files", files[i]);
            }

            // 显示正在提交
            $('#status').removeClass('success error').text('正在提交请求...');

            // 发送AJAX请求
            $.ajax({
                url: 'http://localhost:8080/addPlate',
                type: 'POST',
                data: formData,
                contentType: false,
                processData: false,
                success: function(response) {
                    $('#status').removeClass('error').addClass('success')
                        .html('请求成功! <br>响应: ' + JSON.stringify(response));
                    console.log('成功:', response);
                },
                error: function(xhr, status, error) {
                    $('#status').removeClass('success').addClass('error')
                        .html('请求失败! <br>状态: ' + status + '<br>错误: ' + error);
                    console.error('错误:', error);
                }
            });
        });
    });
</script>
</body>
</html>